#!/usr/bin/env ruby

require 'open-uri'
require 'pathname'

file = "https://raw.githubusercontent.com/postmodern/ruby-versions/master/rubinius/checksums.sha256"
dir = Pathname(File.expand_path("share/ruby-build"))

open(file).each do |package|
  sha256, filename = package.chomp.split
  version = filename.match(/rubinius-(3.[\d\.]+).tar.bz2/)

  next unless version

  version = version[1]
  defname = "rbx-#{version}"

  next if File.exists?(dir.join(defname))

  definition = <<-TEMPLATE
require_llvm 3.7
install_package "openssl-1.0.2o" "https://www.openssl.org/source/openssl-1.0.2o.tar.gz#ec3f5c9714ba0fd45cb4e087301eb1336c317e0d20b575a125050470e8089e4d" mac_openssl --if has_broken_mac_openssl
install_package "rubinius-#{version}" "https://rubinius-releases-rubinius-com.s3.amazonaws.com/rubinius-#{version}.tar.bz2##{sha256}" rbx
  TEMPLATE

  File.open(dir.join(defname), "w"){|f| f.write definition}
end
